package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.Hotel;
import com.wn.common.pojo.MaxAndMinLatAndLng;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.Rsp;
import com.wn.mapper.HotelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class HotelService {

    @Autowired
    private HotelMapper hotelMapper;

    //查询所有hotel详情
    public PageInfo<Hotel> queryAll(PageParam<Hotel> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());

            List<Hotel> hotelList = hotelMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Hotel> hotelPageInfo = new PageInfo<>(hotelList);
            return hotelPageInfo;
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<Hotel> hotelList = hotelMapper.queryAll();
            PageInfo<Hotel> pageInfo = new PageInfo<Hotel>(hotelList);
            return pageInfo;
        }
    }

    //根据主键查询
    public Rsp selectByPrimaryKey(Integer id) {
        Hotel hotel = hotelMapper.selectByPrimaryKey(id);
        if(StringUtils.isEmpty(hotel)){
             return new Rsp().fail().msg("查询失败");
        }else {
            return new Rsp().success(hotel);
        }
    }

    //逻辑删除
    public Rsp deletelogic(Integer id){
        int exe = hotelMapper.deleteByPrimaryKey(id);
        if(exe == 1){
            return Rsp.success(null);
        }else{
            return Rsp.fail();
        }
    }


   //更新数据
    public Rsp updateAll(Hotel hotel){
        int i = hotelMapper.updateByPrimaryKeySelective(hotel);

        if(i == 1){
            return Rsp.success(null).msg("更新成功");
        }else {
            return Rsp.fail().msg("更新失败");
        }
    }

    //增加数据
    public Rsp insterAll(Hotel hotel){
        int i = hotelMapper.insertSelective(hotel);
        if(i == 1){
            return new Rsp().success(i);
        }else{
            return new Rsp().fail().msg("添加失败");
        }
    }

    /**
     * 修改城市的状态
     * @param hotel
     */
    public Rsp updateNewsinfo(Hotel hotel) {

        if (hotel.getStatus() == 0){
            hotel.setStatus(1);
        }else{
            hotel.setStatus(0);
        }

        int i = hotelMapper.updateNewsinfo(hotel.getId(), hotel.getStatus());

        if(i == 1){
            return Rsp.success(null);
        }else {
            return Rsp.fail();
        }
    }


    //    //根据城市计算出周围的景点
//    public List<ScenicSpots> getScenicSpotsByLatAndLng(MaxAndMinLatAndLng maxAndMinLatAndLng) {
//
//        List<ScenicSpots> scenicSpotss = scenicSpotsMapper.getScenicSpotsByLatAndLng(maxAndMinLatAndLng);
//
//        return scenicSpotss;
//    }

    /**
     * 城市周围酒店
     * @param neighPosition
     */
    public List<Hotel> getHotelByLatAndLng(MaxAndMinLatAndLng neighPosition) {

        List<Hotel> hotelByLatAndLng = hotelMapper.getHotelByLatAndLng(neighPosition);

        return hotelByLatAndLng;

    }

}
